home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / BASIC / LIB / EVENTSHELL / EXTENSION / CalLib (.txt) < prev    next >
RISC OS BBC BASIC V Source  |  1996-03-01  |  10KB  |  294 lines

  1.  Module CalLib
  2.  -----------------------------------------------------------------------
  3.  Calendar Routines (version 1.03 01-Mar-96)
  4.  Based on Version 1.20 - (C) Ian Palmer, 1st March 1993, supplied with
  5.  BLibII. various routines added and renamed by Paul Hobbs 28-Oct-94
  6.  This library contains several routines related to the calendar.
  7.  In all cases months are numbered from 1 to 12 (1 = January, etc.)
  8.  and years are the full A.D. year, (eg. 1993, etc.).
  9.  -----------------------------------------------------------------------
  10.  Public Methods Supported:
  11.    FN_shell_CalLib_Init
  12.    FNshell_CalendarDayOfWeek
  13.    FNshell_CalendarWeekDayString
  14.    FNshell_CalendarMonthString
  15.    FNshell_CalendarMonthNumber
  16.    FNshell_CalendarDateString
  17.    FNshell_CalendarDateValid
  18.    FNshell_CalendarDaysInMonth
  19.    FNshell_CalendarDaysInYear
  20.    FNshell_CalendarLeapYear
  21.    FNshell_CalendarDayOfYear
  22.    FNshell_CalendarDaysBetween
  23.    PROCshell_CalendarSetMonthString
  24.    PROCshell_CalendarSetDayString
  25.    PROCshell_CalendarGetTodaysDate
  26.  Private Methods Supported:
  27.       None
  28. & *|Start FN_shell_CalLib_Init
  29. _shell_CalLib_Init
  30.  Initialise Calendar module (constants/variables etc). Returns TRUE if module
  31.  was initialised correctly..
  32.  Define Module Variables
  33. -B_m_Calendar_MonthStr$ = "JanFebMarAprMayJunJulAugSepOctNovDec"
  34. .3_m_Calendar_DayStr$   = "SunMonTueWedThuFriSat"
  35.  Define Module Constants
  36. 1"_c_Calendar_MonthStrErr%  = 99
  37. 2:_c_Calendar_MonthStrLen%  = 
  38. ( _m_Calendar_MonthStr$ )
  39. _c_Calendar_MonthStrErr$  = "String passed to CalendarSetMonthString must be " + 
  40. ( _c_Calendar_MonthStrLen% ) + " characters long"
  41. 5%_c_Calendar_MonthInvalid$ = "***"
  42. 6%_c_Calendar_DayInvalid$   = "***"
  43. 7"_c_Calendar_DateInvalid$  = ""
  44. 85_c_Calendar_DayStrLen% = 
  45. ( _m_Calendar_DayStr$ )
  46. _c_Calendar_DayStrErr$ = "String passed to CalendarSetDayString must be " + 
  47. ( _c_Calendar_DayStrLen% ) + " characters long"
  48. *|Stop FN_shell_CalLib_Init
  49. A,*|Start PROCshell_CalendarSetMonthString
  50. shell_CalendarSetMonthString( month_name_string$ )
  51.  Routine to set the string containing the 3 character month names used by the other routines
  52.  so that foreign language versions of the program can be easily produced (normally string would
  53.  be included in the message file of the application).
  54.  Error if length of specified string is different to the default string..
  55. ( month_name_string$ ) <> _c_Calendar_MonthStrLen% 
  56.  _c_Calendar_MonthStrErr%, _c_Calendar_MonthStrErr$
  57. M._m_Calendar_MonthStr$ = month_name_string$
  58. Q+*|Stop PROCshell_CalendarSetMonthString
  59. U**|Start PROCshell_CalendarSetDayString
  60. shell_CalendarSetDayString( day_name_string$ )
  61.  Routine to set the string containing the 3 character day names used by the other routines
  62.  so that foreign language versions of the program can be easily produced (normally string would
  63.  be included in the message file of the application).
  64.  Error if length of specified string is different to the default string..
  65. ( day_name_string$ ) <> _c_Calendar_DayStrLen% 
  66.  _c_Calendar_MonthStrErr%, _c_Calendar_DayStrErr$
  67. a*_m_Calendar_DayStr$ = day_name_string$
  68. e)*|Stop PROCshell_CalendarSetDayString
  69. i%*|start FNshell_CalendarDayOfWeek
  70. shell_CalendarDayOfWeek( day%, month%, year% )
  71.  Returns the day of the week for a given date : 1 = Sun, 2 = Mon, etc.
  72.  Q%, N%, X%
  73. N% = month%
  74. X% = year%
  75.  month% <= 2 
  76.  N% = month% + 12 : X% = year% - 1
  77. rTQ% = ( day% + 2.6 * ( N% + 1) + X% + ( X% 
  78.  4 ) - (X% 
  79.  100) + (X% 
  80.  400 ) ) 
  81.  year% < 1752 
  82.  ( year% = 1752 
  83.  ( month% < 9 
  84.  ( month% = 9 
  85.  day% < 3 ) ) ) 
  86.   Q% = Q% - 3
  87.  Q% < 1 
  88.   Q% = Q% + 7
  89. shell_CalendarDateValid( day%, month%, year% ) ) 
  90.   Q% = 0
  91. ~$*|stop FNshell_CalendarDayOfWeek
  92. )*|start FNshell_CalendarWeekDayString
  93. shell_CalendarWeekDayString( day_nr% )
  94.  Returns a three letter version of a day of the week (1 -> "Sun",
  95.  2 -> "Mon", etc.)
  96.  day_nr% < 1 
  97.  day_nr% > 7 
  98.  = _c_Calendar_DayInvalid$
  99.  _m_Calendar_DayStr$, ( day_nr% - 1) * 3 + 1, 3 )
  100. (*|stop FNshell_CalendarWeekDayString
  101. '*|start FNshell_CalendarMonthString
  102. shell_CalendarMonthString( month_nr% )
  103.  Returns a three letter version of a month (1 -> "Jan", 2 -> "Feb", etc.)
  104.  month_nr% < 1 
  105.  month_nr% > 12 
  106.  = _c_Calendar_MonthInvalid$
  107.  _m_Calendar_MonthStr$, ( month_nr% - 1 ) * 3 + 1, 3 )
  108. &*|stop FNshell_CalendarMonthString
  109. '*|start FNshell_CalendarMonthNumber
  110. shell_CalendarMonthNumber( month$ )
  111.  Returns a  month number given a 3 letter string ("Jan" -> 1, "Feb" -> 2, etc.)
  112.  result%
  113. ( month$ ) <> 3 
  114.  _m_Calendar_MonthStr$, month$ ) 
  115. =  result% = ( 
  116.  _m_Calendar_MonthStr$, month$ ) + 2 ) / 3
  117.   result% = 0
  118. = result%
  119. &*|stop FNshell_CalendarMonthNumber
  120. &*|start FNshell_CalendarDateString
  121. shell_CalendarDateString( day_nr%, month_nr%, year% )
  122.  Given a date, returns a string version eg.
  123.  FNshell_CalendarDateString( 18, 10, 1991 ) :
  124.  Fri 18th Oct 1991
  125.  day_of_week%, date_string$, N%
  126. date_string$ = ""
  127. shell_CalendarDateValid( day_nr%, month_nr%, year% ) 
  128. K  day_of_week% =  
  129. shell_CalendarDayOfWeek( day_nr%, month_nr%, year% )
  130. H  date_string$ =  
  131. shell_CalendarWeekDayString( day_of_week% ) + " "
  132.  day_nr% < 10 
  133.     date_string$ += " "
  134. "  date_string$ += 
  135. ( day_nr% )
  136.   N% =  day_nr% 
  137.  N% = 1 
  138.  day_nr% <> 11 date_string$ += "st" : 
  139.  N% = 2 
  140.  day_nr% <> 12 date_string$ += "nd" : 
  141.  N% = 3 
  142.  day_nr% <> 13 date_string$ += "rd" : 
  143.  date_string$ += "th"
  144. V  date_string$ += " " + 
  145. shell_CalendarMonthString( month_nr% ) + " " + 
  146. ( year% )
  147. -  date_string$ = _c_Calendar_DateInvalid$
  148. = date_string$
  149. %*|stop FNshell_CalendarDateString
  150. %*|start FNshell_CalendarDateValid
  151. shell_CalendarDateValid( day_nr%, month_nr%, year% )
  152.  Given a date returns a boolean value TRUE date is valid,
  153.  FALSE date is not valid.
  154.  day_nr% < 1   
  155.  month_nr% < 1 
  156.  year% < 1     
  157.  day_nr% > 30 
  158.  ( month_nr% = 4 
  159.  month_nr% = 6 
  160.  month_nr% = 9 
  161.  month_nr% = 11 ) 
  162.  day_nr% > 31 
  163.  month_nr% > 12 
  164.  month_nr% = 2 
  165.  day_nr% > 29 
  166.  month_nr% = 2 
  167.  day_nr% = 29 
  168.  ( year% 
  169.  4 ) <> 0 
  170.  month_nr% = 2 
  171.  day_nr% = 29 
  172.  ( year% 
  173.  100 ) = 0 
  174.  ( year% 
  175.  400 ) > 0 
  176.  handle the missing days in September 1752 when calendar was changed over..
  177.  year% = 1752 
  178.  month_nr% = 9 
  179.  day_nr% > 2 
  180.  day_nr% < 14 
  181. $*|stop FNshell_CalendarDateValid
  182. '*|Start FNshell_CalendarDaysInMonth
  183. shell_CalendarDaysInMonth( month_nr%, year% )
  184.  Given a month number, returns number of days in the given month.
  185.  nr_of_days%
  186.  month_nr% > 0 
  187.  year% > 0 
  188.  month_nr% >= 1 
  189.  month_nr% <= 12 
  190.  month_nr% 
  191.       
  192.  4,6,9,11:
  193.         nr_of_days% = 30
  194.       
  195. 0        
  196. shell_CalendarLeapYear( year% ) 
  197.           nr_of_days% = 29
  198.         
  199.           nr_of_days% = 28
  200.         
  201.       
  202.         nr_of_days% = 31
  203.         
  204.     nr_of_days% = 0
  205. = nr_of_days%
  206. &*|Stop FNshell_CalendarDaysInMonth
  207.     &*|start FNshell_CalendarDaysInYear
  208. shell_CalendarDaysInYear( year% )
  209.  Given a year, returns number of days in the given year.
  210.  result%
  211.  year% > 0 
  212.  year% = 1752 
  213.     result% = 355
  214. 8    result% = 365 - 
  215. shell_CalendarLeapYear( year% )
  216.   result% = 0
  217. = result%
  218. %*|stop FNshell_CalendarDaysInYear
  219. $*|start FNshell_CalendarLeapYear
  220. shell_CalendarLeapYear( year% )
  221.  TRUE if given year is a leap year, FALSE if not.
  222.  result%
  223.  year% > 1 
  224. 'U  result% = ( ( year% 
  225.  4 ) = 0 
  226.  ( ( year% 
  227.  100 ) > 0 
  228.  ( year% 
  229.  400 ) = 0 ) )
  230.   result% = 0
  231. = result%
  232. -#*|stop FNshell_CalendarLeapYear
  233. 1%*|start FNshell_CalendarDayOfYear
  234. shell_CalendarDayOfYear( D%, M%, Y% )
  235.  Given a date, returns the what day of that year that date is
  236.  (ie. 1 to 366), or 0 if date is invalid
  237.  day_of_year%
  238. shell_CalendarDateValid( D%, M%, Y% ) 
  239.   day_of_year% = D%-31*(M%>1)-28*(M%>2)-31*(M%>3)-30*(M%>4)-31*(M%>5)-30*(M%>6)-31*(M%>7)-31*(M%>8)-30*(M%>9)-31*(M%>10)-30*(M%>11)-((M%>2) 
  240. shell_CalendarLeapYear(Y%))+11*(Y%=1752 
  241.  ((M%=9 
  242.  D%>13) 
  243.  M%>9))
  244.   day_of_year% = 0
  245. = day_of_year%
  246. A$*|stop FNshell_CalendarDayOfYear
  247. E'*|start FNshell_CalendarDaysBetween
  248. shell_CalendarDaysBetween( D%, M%, Y%, d%, m%, y% )
  249.  Given two dates, this function returns the number of days between
  250.  them. If the first date is after the second, then the result will
  251.  be negative. If either date is invalid then returns 0.
  252.  A%, B%, T%, N%, nr_days%
  253. T% = 0
  254. N% = 1
  255. shell_CalendarDateValid( D%, M%, Y% ) 
  256. shell_CalendarDateValid( d%, m%, y% ) 
  257.  y% < Y% 
  258.  ( Y% = y% 
  259.  m% < M% ) 
  260.  ( Y% = y% 
  261.  m% = M% 
  262.  d% < D% ) 
  263.  N% = -1 : 
  264.  D%,d % : 
  265.  M%, m% : 
  266.  Y%, y%
  267.  Y% < y% 
  268.  A% = Y% 
  269.  ( y% - 1 )
  270. U2     T% = T% + 
  271. shell_CalendarDaysInYear( A% )
  272. V        
  273. Xn  nr_days% = N% * ( T% + 
  274. shell_CalendarDayOfYear( d%, m%, y% ) - 
  275. shell_CalendarDayOfYear( D%, M%, Y% ) )
  276.   nr_days% = 0
  277. = nr_days%
  278. _&*|stop FNshell_CalendarDaysBetween
  279. c+*|start PROCshell_CalendarGetTodaysDate
  280. shell_CalendarGetTodaysDate( 
  281.  day%, 
  282.  month%, 
  283.  year% )
  284.  Fills the three variables passed to it with today's date, taken from TIME$
  285.  Will very likely fail if TIME$ is not in default English format..
  286. day%   = 
  287. $,  5 ) )
  288. year%  = 
  289. $, 12 ) )
  290. kAmonth% = ( 
  291.  _m_Calendar_MonthStr$, 
  292. $, 8, 3  ) ) + 2 ) 
  293. o**|stop PROCshell_CalendarGetTodaysDate
  294.